﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace mirror
{
    public class EnemyActor : Actor
    {
        static Random r = new Random();
        Actor playerActor;
        //float health = 2.0f;
        const float INITIAL_HEALTH = 2.0f;
        //static const float SPAWN_X_MAX =;
        //static const float SPAWN_X_MIN =;
        //static const float SPAWN_Y_MAX =;
        //static const float SPAWN_Y_MIN =;

        //TODO
        //sound deathSound;
        //sound hitSound;

        public EnemyActor(Model model, Vector2 position, Actor _playerActor) : base(model, position)
        {
            playerActor = _playerActor;
            health = INITIAL_HEALTH;
        }

        //*
        public override void Update()
        {
            if (!isDead)
            {
                Vector2 dirToPlayer = playerActor.pos - pos;
                //*
                if (dirToPlayer.LengthSquared() < 10.0f)
                {
                    acc += dirToPlayer * 0.01f;
                }
                else
                {
                    acc += dirToPlayer * 0.001f;
                }
                //*/
                float inv = (1.0f / dirToPlayer.LengthSquared());
                acc += dirToPlayer * inv * 0.1f;
                acc += new Vector2((float)r.NextDouble() - 0.5f,
                                    (float)r.NextDouble() - 0.5f
                                  );
                acc *= 5.0f;

                heading = 0.5f * (float)(Math.Atan2(vel.X, vel.Y));//+ MathHelper.ToRadians(90.0f) );

                base.Update();
            }
            else
            {
                //todo
                //play death sfx

                // respawn
                health = INITIAL_HEALTH;
                playerActor.score += 100;
                Vector2 respawnPos = new Vector2(0.0f, 0.0f);
                //do
                {
                    respawnPos.X = (float)r.NextDouble();
                    respawnPos.Y = (float)r.NextDouble();
                }// while ((playerActor.pos - respawnPos).Length() > 10.0f); //todo
                pos = respawnPos;
                vel = new Vector2(0, 0);
                acc = new Vector2(0, 0);
            }
        }

        public override void Render()
        {
            base.Render();
        }
    };
}